﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using SmartSpace.Documents;

namespace SmartSpace.Documents.Services
{
    public class DocServices : IDocServices
    {
        public ServiceResult SaveDocument(Document doc)
        {
            using (DocumentsDbContext context = new DocumentsDbContext())
            {
                if (context.Set<Document>().Where(p => p.Id == doc.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    //exist not in db --> add
                    context.Set<Document>().Add(doc);
                }
                else
                {
                    context.Entry(doc).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "Save Document Success");
                }
                else
                {
                    Logger.Debug("document", String.Format("Save document {0},{1} failure", doc.Id, doc.FileName + "." + doc.Extension));
                    return new ServiceResult(false, "Save Document Failure");
                }
            }
        }

        public ServiceResult DeleteDocument(Guid docId)
        {
            using (DocumentsDbContext context = new DocumentsDbContext())
            {
                SingleResult<Document> result = GetDocument(docId);

                if (result.Data == null)
	            {
		            Logger.Debug("document", String.Format("Delete document {0} failure",docId));
                    return new ServiceResult(false, "Delete Document Failure");
	            }
                else
                {
                    context.Entry(result.Data).State = System.Data.Entity.EntityState.Deleted;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "Delete Document Success");
                }
                else
                {
                    return new ServiceResult(false, "Delete Document Failure");
                }
            }
        }

        public ServiceResult MoveDocument(Guid docId, Guid folderId)
        {
            using (DocumentsDbContext context = new DocumentsDbContext()) 
            {
                SingleResult<Document> result = GetDocument(docId);
                Document document = result.Data;
                if (result.Data == null)
                {
                    Logger.Debug("document", String.Format("get document {0} failure", docId));
                    return new ServiceResult(false, "Get Document Failure");
                }
                else {
                    document.FolderId = folderId;
                    ServiceResult sresult = SaveDocument(document);
                    if (sresult.IsSuccess == true)
                    {
                        return new ServiceResult(true, "Move Document Success");
                    }
                    else
                    {
                        return new ServiceResult(false, "Move Document Success");
                    }
                }
            }
        }

        public SingleResult<Document> GetDocument(Guid docId)
        {
            using (DocumentsDbContext context = new DocumentsDbContext())
            {
                Document document = new Document();
                document = context.Set<Document>().Where(p => p.Id == docId).SingleOrDefault();
                if (document == null)
                {
                    return new SingleResult<Document>(false, "Get document failure", null);
                }
                else
                {
                    return new SingleResult<Document>(document);
                }
            }
        }

        public ServiceResult SaveFolder(Folder folder)
        {
            using (DocumentsDbContext context = new DocumentsDbContext())
            {
                if (context.Set<Folder>().Where(p => p.Id == folder.Id).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    //exist not in db --> add
                    context.Set<Folder>().Add(folder);
                }
                else
                {
                    context.Entry(folder).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return new ServiceResult(true, "Save Folder Success");
                }
                else
                {
                    Logger.Debug("Folder", String.Format("Save folder {0},{1} failure", folder.Id, folder.FolderName));
                    return new ServiceResult(false, "Save Folder Failure");
                }
            }
        }

        public ServiceResult DeleteFolder(Guid folderId)
        {
            using (DocumentsDbContext context = new DocumentsDbContext())
            {
                Folder folder = context.Set<Folder>().Where(p => p.Id == folderId).SingleOrDefault();
                if (folder != null)
                {
                    context.Entry(folder).State = System.Data.Entity.EntityState.Deleted;
                    if (context.SaveChanges() > 0)
                    {
                        return new ServiceResult(true, "delete folder sccuss!");
                    }
                    else
                    {
                        Logger.Debug("Folder", string.Format("delete folder {0},{1} failure", folder.Id, folder.FolderName));
                        return new ServiceResult(false, "error");
                    }
                }

                return new ServiceResult(true, "delete folder sccuss, article is not in the db!");
            }
        }

        public List<Document> GetDocsByFolderId(Guid id)
        {
            String sql = "select * from doc_document";

            DataTable table = Common.GetDataTableNoParm(sql);
            try
            {
                return Common.DataTableToList<Document>(table);
            }
            catch {
                Logger.Debug("Document", "Convert Table to List error!");
                return null;
            }
        }

        public DataTable GetAllFolders()
        {
            String sql = "select * from fol_folder";
            DataTable table = Common.GetDataTableNoParm(sql);
            try
            {
                return table;
            }
            catch
            {
                Logger.Debug("Folder", "Get All Folders Error!");
                return null;
            }
        }

        public Folder GetFolderById(Guid id)
        {
            using (DocumentsDbContext context = new DocumentsDbContext())
            {
                Folder folder = new Folder();
                folder = context.Set<Folder>().Where(p => p.Id == id).SingleOrDefault();
                if (folder == null)
                {
                    return null;
                }
                else
                {
                    return folder;
                }
            }
        }
    }
}
